﻿/* Copyright (C) 2012 by John Cronin
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace davserv
{
    class uri
    {
        List<string> members = new List<string>();

        public static uri Parse(string path)
        {
            uri ret = new uri();

            path = path.Replace("\\", "/");
            string[] paths = path.Split('/');

            foreach (string p in paths)
            {
                if ((p == ".") || (p == ""))
                    continue;
                else if (p == "..")
                {
                    if (ret.members.Count > 0)
                        ret.members.RemoveAt(ret.members.Count - 1);
                    else throw new Exception("Invalid uri: " + path);
                }
                else
                    ret.members.Add(p);
            }

            return ret;
        }

        string tostr()
        {
            if (members.Count == 0)
                return "/";

            StringBuilder sb = new StringBuilder();
            foreach (string s in members)
            {
                sb.Append("/");
                sb.Append(s);
            }

            return sb.ToString();
        }

        public override string ToString()
        {
            return tostr();
        }

        public uri Trim()
        {
            uri ret = new uri { members = new List<string>(members) };
            if (ret.members.Count > 0)
                ret.members.RemoveAt(ret.members.Count - 1);
            return ret;
        }

        public uri TrimStart(uri b)
        {
            /* Trim some members from the start of the current uri if
             * they all match (in sequence) those in b
             * 
             * e.g.     (A, B, C).Trim(A) = (B, C)
             *          (A, B, C).Trim(A, B) = (C)
             *          (A, B, C).Trim(B, A) = (A, B, C)    // no trim as not in order
             *          
             */

            uri ret = new uri { members = new List<string>(members) };

            for (int i = 0; i < b.members.Count; i++)
            {
                if (ret.members[i] != b.members[i])
                    return ret;
            }

            for (int i = 0; i < b.members.Count; i++)
                ret.members.RemoveAt(0);

            return ret;
        }

        public uri Append(string path)
        {
            uri ret = new uri { members = new List<string>(members) };
            ret.members.Add(path);
            return ret;
        }

        public uri Append(uri path)
        {
            uri ret = new uri { members = new List<string>(members) };
            ret.members.AddRange(path.members);
            return ret;
        }
    }
}
